package com.example.demo.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.example.demo.entity.Areas;
import com.example.demo.entity.SendSmsEntity;
import com.example.demo.entity.Users;
import com.example.demo.service.AreaService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SendSms {
  @Autowired
  private UserService userService;

  @Autowired
  private AreaService areaService;

  public static void main(String[] args) throws Exception {
    SendSms sendSms = new SendSms();
//    SendSmsEntity sendSmsEntity = new SendSmsEntity();
//    sendSmsEntity.setAreaName("普陀区");
//    sendSmsEntity.setCompanyName("上海市有限公司");
//    sendSmsEntity.setItemType("新办");
//    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//    String dateStr = sdf.format(new Date());
//    sendSmsEntity.setSubmitTime(dateStr);
//    sendSmsEntity.setPhoneNumbers("13991477009");
//    sendSms.sengSms(sendSmsEntity);
//    sendSms.SendSmsListTest();
  }


  public void SendSmsList(List<SendSmsEntity> list) {

    for (int i = 0; i < list.size(); i++) {
      SendSmsEntity sendSmsEntity = list.get(i);
      if (sendSmsEntity.getItemType() != null
              && sendSmsEntity.getAreaName() != null
              && sendSmsEntity.getSubmitTime() != null
              && sendSmsEntity.getCompanyName() != null
              && sendSmsEntity.getPhoneNumbers() != null)
      {
        String result = this.sengSms(sendSmsEntity);

      }
    }
  }


  public String sengSms(SendSmsEntity sendSmsEntity) {
    //需写入注册账号的accessKeyid和secret编码
    //如上海追索科技有限公司为
    // accessKeyid：LTAI4G9vbafcUsEz6f7bPMAm
    // secret：Mn77ZnZF4DhdasOYg8ST91mh0ye3OJ
    DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G9vbafcUsEz6f7bPMAm", "Mn77ZnZF4DhdasOYg8ST91mh0ye3OJ");
    IAcsClient client = new DefaultAcsClient(profile);

    CommonRequest request = new CommonRequest();
    request.setSysMethod(MethodType.POST);
    request.setSysDomain("dysmsapi.aliyuncs.com");//不做更改
    request.setSysVersion("2017-05-25");//不做更改
    request.setSysAction("SendSms");//不做更改，业务名称（短信）
    request.putQueryParameter("RegionId", "cn-hangzhou");
    request.putQueryParameter("PhoneNumbers", sendSmsEntity.getPhoneNumbers());
    request.putQueryParameter("SignName", "上海粮食物资储备一网通办");
    request.putQueryParameter("TemplateCode", "SMS_206745305");

    request.putQueryParameter("TemplateParam",
            "{\"companyName\":\"" + sendSmsEntity.getCompanyName() +
                    "\",\"submitTime\":\"" + sendSmsEntity.getSubmitTime() +
                    "\",\"areaName\":\"" + sendSmsEntity.getAreaName() +
                    "\",\"itemType\":\"" + sendSmsEntity.getItemType() + "\"}");
    try {
      CommonResponse response = client.getCommonResponse(request);
      System.out.println(response.getData());
      System.out.println("短信发送结果："+"发送成功");
      return "发送成功";
    } catch (ServerException e) {
      e.printStackTrace();
      System.out.println("短信发送结果："+"发送失败");
      return "发送失败";
    } catch (ClientException e) {
      e.printStackTrace();
      System.out.println("短信发送结果："+"连接异常");
      return "连接异常";
    }
  }

}


